<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitereview
 * @copyright  Copyright 2012-2013 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Controller.php 6590 2013-04-01 00:00:00Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitereview_Widget_ReviewButtonController extends Engine_Content_Widget_Abstract {

  public function indexAction() {

    $listing_guid = $this->_getParam('listing_guid', null);
    $identity = $this->_getParam('identity', 0);
    $this->view->listing_profile_page = $this->_getParam('listing_profile_page', 0);
    if (empty($listing_guid) && !Engine_Api::_()->core()->hasSubject('sitereview_listing')) {
      return $this->setNoRender();
    }
    
    if(empty($listing_guid) && Engine_Api::_()->core()->hasSubject('sitereview_listing')) {
      $this->view->sitereview = $sitereview = Engine_Api::_()->core()->getSubject('sitereview_listing');
      $listing_guid = $sitereview->getGuid();
      $this->view->listing_profile_page = 1;
      $identity = Engine_Api::_()->sitereview()->existWidget('sitereview_reviews', 0, $sitereview->listingtype_id);
    }
    else {
      $this->getElement()->removeDecorator('Title');
      $this->getElement()->removeDecorator('Container');
    }

    $this->view->sitereview = $sitereview = Engine_Api::_()->getItemByGuid($listing_guid);
    $this->view->listingtype_id = $listingtype_id = $sitereview->listingtype_id;
    Engine_Api::_()->sitereview()->setListingTypeInRegistry($listingtype_id);
    $this->view->listingtypeArray = $listingtypeArray = Zend_Registry::get('listingtypeArray' . $listingtype_id);
    if (empty($listingtypeArray->reviews) || $listingtypeArray->reviews == 1) {
      return $this->setNoRender();
    }

    //GET VIEWER
    $this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer();
    $viewer_id = $viewer->getIdentity();
    //GET USER LEVEL ID
    if (!empty($viewer_id)) {
      $this->view->level_id = $level_id = $viewer->level_id;
    } else {
      $this->view->level_id = $level_id = Engine_Api::_()->getDbtable('levels', 'authorization')->fetchRow(array('type = ?' => "public"))->level_id;
    }

    $create_review = ($sitereview->owner_id == $viewer_id) ? $listingtypeArray->allow_owner_review : 1;
    if (empty($create_review)) {
      return $this->setNoRender();
    }

    $create_allow = Engine_Api::_()->authorization()->getPermission($level_id, 'sitereview_listing', "review_create_listtype_$listingtype_id");

    if ($create_allow) {
      //GET REVIEW TABLE
      $reviewTable = Engine_Api::_()->getDbTable('reviews', 'sitereview');
      if ($viewer_id) {
        $params = array();
        $params['resource_id'] = $sitereview->listing_id;
        $params['resource_type'] = $sitereview->getType();
        $params['viewer_id'] = $viewer_id;
        $params['type'] = 'user';
        $hasPosted = $reviewTable->canPostReview($params);
      } else {
        $hasPosted = 0;
      }
      $this->view->createAllow = empty($hasPosted) ? 1 : 2;
    }

    if ($this->view->listing_profile_page) {
      $this->view->contentDetails = Engine_Api::_()->sitereview()->getWidgetInfo('sitereview.user-sitereview', $identity);
    } 
  }

}